/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TextManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * This class is supposed to hold all words in every document, containing every
 * possible word to be found.
 * This class is very similar to the WordOccurrenceMap class, except that it does
 * not contain the information of how many times each word has occurred.
 * @author Jaime
 */
public class Vocabulary implements WordOccurrenceHolder, Iterable<WordOccurrence>{
    
    private List<WordOccurrence> words;
    private HashMap<WordOccurrence, WordOccurrence> wordsMap;
    
    public Vocabulary()
    {
        words = new ArrayList<WordOccurrence>();
        wordsMap = new HashMap<WordOccurrence, WordOccurrence>();
    }
    
    public void newOccurrence(WordOccurrence word)
    {
        if(!hasWord(word))
        {
            wordsMap.put(word, word);
            words.add(word);
        }
    }

    public boolean hasWord(WordOccurrence word) {
        return wordsMap.containsKey(word);
    }
    
    public int size()
    {
        return words.size();
    }
    
    public String toString()
    {
        String ret = "Vocabulary with "+words.size()+" words:\n";
        
        int wordsPerLine = 10;
        int i = 0;
        
        for(WordOccurrence word:words)
        {
            ret += word.getContent();
            
            if(i%wordsPerLine == 0 && i != 0)
            {
                ret += "\n";
            }
            
            if(i%wordsPerLine != 0)
            {
                ret+= ", ";
            }
            
            i++;
        }
        
        return ret;
        
    }

    @Override
    public Iterator<WordOccurrence> iterator() {
        return words.iterator();
    }
    
}
